// document.addEventListener('DOMContentLoaded', function() {
//     const urlTask ="http://10.62.27.21/"
//     const link = document.querySelector('.nav-link[href="urlTask"]')
//     link.setAttribute('href', `${urlTask}task`)
//   })



// API 说明
const metaURL = "http://10.62.27.21/apim/"
// const url ="https://10.62.4.147:53001/"
const apiArea = document.querySelector('div.api')
apiArea.insertAdjacentHTML('afterend', `<div class="code-area">
<pre><code>    <span class="badge">POST</span> <span class="code-string">${metaURL}source/changebasemeta</span><span class="code-note">//基础数据库更新地址</span>
    <span class="badge">POST</span> <span class="code-string">${metaURL}source/changeimgmeta</span><span class="code-note">//影像数据库更新地址</span></code></pre>

</div>`);
const swagger = document.querySelector('a.swagger')
swagger.href = `${metaURL}swagger/index.html`
const sendIp = document.querySelector('code span.sendIp')
sendIp.innerHTML = `"${metaURL}source/changeimgmeta"`



// 获取所有的 code-copy 按钮
const codeCopyButtons = Array.from(document.querySelectorAll('button.code-copy'));

// 为每个按钮添加点击事件
codeCopyButtons.forEach(( button, index ) => {
  button.addEventListener('click', () => {
    const codeElement = document.querySelectorAll('code')[index];

    // 创建一个临时 textarea 元素
    const textarea = document.createElement('textarea');
    textarea.value = codeElement.innerText;
    document.body.appendChild(textarea);

    // 选择临时元素的内容
    textarea.select();

    // 将内容复制到剪贴板
    document.execCommand('copy');

    // 删除临时元素
    document.body.removeChild(textarea);

    alert('复制成功!')

    const icon = button.querySelector('i')
    icon.classList.remove('bi-clipboard')
    icon.classList.add('bi-check2')

    setTimeout(() => {
      icon.classList.remove('bi-check2')
      icon.classList.add('bi-clipboard')
    }, 2000)

  });
});



// 为body标签添加，提示弹窗popArea
const body = document.querySelector('body');
const popArea = document.createElement('div')
popArea.classList.add('popArea')
body.append(popArea)

const alert = (message) => {
  const pop = document.createElement('div')
  pop.classList.add("pop")
  pop.innerHTML = [
    `   <div><i class="bi-check2"></i>&nbsp;&nbsp;${message}</div>`,
  ].join('')
  popArea.prepend(pop)

  setTimeout(() => {
    pop.style.opacity = 0
    setTimeout(() => {
      popArea.removeChild(pop)
    }, 500)
  }, 2000)

}


// 表格
const dataURL = "http://10.62.27.21/apid/"
// const dataURL ='https://10.62.4.147:51001/'
let metaType = []
let fileType = []
let metaTypeContent = ''
let fileTypeContent = ''
const insertContent = (array) => {
  let content = ''
  array.forEach(item => content += `${item.code}为${item.name}、`)
  return content
}
getMetaType()
async function getMetaType() {
    await request.get(`${dataURL}sys/metatype`)
    .then((res) => {
      metaType = res.data.data
      metaTypeContent = insertContent(metaType)
      datameta[6].des += `${metaTypeContent.slice(0,-1)}`
      table(datameta, 'datameta')
    })
    .catch((error) => {
      console.log(error)
      table(datameta, 'datameta')
    })
    
}
getFileType()
async function getFileType() {
  const res = await request.get(`${dataURL}sys/filetype`)
  .then((res) => {
    fileType = res.data.data
    fileTypeContent = insertContent(fileType)
    datafile[7].des += `${fileTypeContent.slice(0,-1)}`
    table(datafile, 'datafile')
  })
  .catch((error) => {
    console.log(error)
    table(datafile, 'datafile')
  })
}
const datameta = [
  { _id: "字符串", must: "否", des: "ID，为空字符，系统自动生成" },
  { createby: "字符串", must: "否", des: "创建者，为空字符，系统自动生成" },
  { createtime: "时间", must: "否", des: "创建时间，为空字符，系统自动生成" },
  { updatetime: "时间", must: "否", des: "更新时间，为空字符，系统自动生成" },
  { uid: "字符串", must: "是", des: "元数据唯一标识码，格式为 GUID" },
  { targname: "字符串", must: "是", des: "目标名称" },
  { type: "整型", must: "是", des: `元数据类型码，其中：` },
  { time: "时间", must: "是", des: "数据时相" },
  { geo: "字符串", must: "否", des: "数据矢量范围，格式为 GeoJSON" },
  { geoid: "字符串", must: "否", des: "数据矢量唯一标识码，与 uid 一致" },
  { metafeature: "字符串", must: "否", des: "元数据特征" },
  { img_cloudperce: "浮点型", must: "否", des: "影像云量，取值范围：0-100 <strong>（影像数据库更新接口）</strong>" },
  { img_orbitid: "字符串", must: "否", des: "影像轨道号 <strong>（影像数据库更新接口）</strong>" },
  { img_scenepath: "字符串", must: "否", des: "纵向条带号 <strong>（影像数据库更新接口）</strong>" },
  { img_acenerow: "字符串", must: "否", des: "横向条带号 <strong>（影像数据库更新接口）</strong>" },
]
const datafile = [
  { _id: "字符串", must: "否", des: "ID，为空字符，系统自动生成" },
  { createby: "字符串", must: "否", des: "创建者，为空字符，系统自动生成" },
  { createtime: "时间", must: "否", des: "创建时间，为空字符，系统自动生成" },
  { updatetime: "时间", must: "否", des: "更新时间，为空字符，系统自动生成" },
  { uid: "字符串", must: "是", des: "文件唯一标识码，格式为 GUID" },
  { metaid: "字符串", must: "是", des: "元数据唯一标识码，与 datameta 的 uid 一致" },
  { name: "字符串", must: "是", des: "文件名称" },
  { type: "整型", must: "是", des: `文件类型码，其中：` },
  { isfile: "布尔型", must: "是", des: "是否是文件，<strong>若 isfile 为 true，则 extension、shareurl、size、sorpath、md5 为必填项</strong>" },
  { extension: "字符串", must: "否", des: "文件扩展名" },
  { shareurl: "字符串", must: "否", des: "成果发布地址" },
  { size: "长整型", must: "否", des: "文件大小" },
  { sorpath: "字符串", must: "否", des: "文件路径" },
  { md5: "字符串", must: "否", des: "完整性验证码" },
  { filefeature: "字符串", must: "否", des: "文件特征值，示例：'ZY3-1,MUX'、'ZY3-1,PAN'。<strong>特征值类型详情见下</strong>" },
]
const data = [
  { sourcecode: "字符串", must: "是", des: "资源类型码", },
  { apikey: "字符串", must: "是", des: "API 请求服务权限标识", },
  { datameta: "数组", must: "是", des: `资源元数据，数组元素为资源元数据对象。<strong>详情见下</strong>`, },
  { datafile: "数组", must: "是", des: "资源数据文件，数组元素为资源数据文件对象。<strong>详情见下</strong>", },
]
table(data, 'data')

function table(data, className) {//传输数据与表格类名
  const tbody = document.querySelector(`table.${className} tbody`)
  let index = 1
  data.forEach(item => {
    const row = document.createElement('tr')
    const keys = Object.keys(item)

    //1
    // const cell = document.createElement('th')
    // cell.textContent = index++
    // row.appendChild(cell)

    //2
    const cell2 = document.createElement('td')
    cell2.innerHTML = keys[0]
    row.appendChild(cell2)

    //3 4
    for (var key in item) {//遍历对象中的属性
      const cell = document.createElement('td')
      cell.innerHTML = item[key]//属性值
      row.appendChild(cell)
    }

    // 将行添加到 tbody 中
    tbody.appendChild(row);
  })
}




// 获取所有图片链接和单独元素
const imageLinks = Array.from(document.querySelectorAll('a.img'));
let currentIndex = 0
const overlay = document.createElement('div');
overlay.classList.add('overlayArea')
overlay.innerHTML = `<img class="enlargeImg">
                    <button type="button" class="previous"><i class="bi-chevron-left"></i></button>
                    <button type="button" class="next"><i class="bi-chevron-right"></i></button>`

// 为每个图片链接添加点击事件
imageLinks.forEach((item, index) => {
  item.addEventListener('click', (e) => {
    // 阻止链接默认行为
    e.preventDefault();

    //body添加overlay
    body.append(overlay)

    // 获取被点击图片的源图片路径，获取图片标签
    const sourceImage = item.href;
    const img = overlay.querySelector('img')

    // 修改单独元素的图片路径为被点击图片的源图片路径
    img.src = sourceImage;

    //保存图片索引
    currentIndex = index
  });
});

// 点击单独元素时隐藏它
overlay.addEventListener('click', () => {
  body.removeChild(overlay)
});

// 点击下一个按钮
const nextButton = overlay.querySelector('.next');
nextButton.addEventListener('click', (e) => {
  e.stopPropagation(); // 阻止事件冒泡，防止点击按钮时触发下面的点击事件
  currentIndex = (currentIndex + 1) % imageLinks.length;//取模运算
  overlay.querySelector('img').src = imageLinks[currentIndex].href;
});
// 点击上一个按钮
const previousButton = overlay.querySelector('.previous');
previousButton.addEventListener('click', (e) => {
  e.stopPropagation(); // 阻止事件冒泡，防止点击按钮时触发下面的点击事件
  currentIndex = (currentIndex - 1 + imageLinks.length) % imageLinks.length;//取模运算
  overlay.querySelector('img').src = imageLinks[currentIndex].href;
});

